package com.ccl.iot.privates;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.ccl.iot.publics.Res;
import com.ccl.iot.dialogs.NoTitleDialog;
import com.ccl.iot.R;
import com.ccl.iot.template.ImageButtonView;

public class DimmerTurnSpeedDialog extends NoTitleDialog {

	private Context mContext;
	private View mView;
	private DimmerTurnSpeed mDimmer;

	private SeekBar mCloseSb, mOpenSb, mDimmerSb;
	private TextView mCloseTv, mOpenTv, mDimmerTv, mDevName;
	private Button mCancelBtn;
	private Button mOkBtn;

	public interface DimmerTurnSpeed{
		/**
		 * 获取设备名字
		 * @return
		 */
		public String getDeviceName();
		
		/**
		 * 获取设备开关速度
		 * @return	开关速度(开关各为一个字节)
		 */
		public short getTurnSpeed();
		
		/**
		 * 获取开启亮度
		 * @return
		 */
		public short getTurnDimmer();
		
		/**
		 * 设置开关速度
		 * @param speed	开关速度各为一个字节, 共两个字节
		 */
		public void setTurnSpeed(byte[] speed);
		
		/**
		 * 设置亮度
		 * @param state
		 * @param isStop
		 */
		public void setDimmer(int state, boolean isStop);
		
		/**
		 * 开始设置亮度
		 */
		public void onStartDimmer();
	}

	public DimmerTurnSpeedDialog(Context context, DimmerTurnSpeed dimmer) {
		super(context);
		this.mContext = context;
		mDimmer = dimmer;
		init();
		initView();
		initData();
		initListener();
	}

	private void init() {
		mView = View.inflate(mContext, R.layout.pop_dimmer_turn_speed, null);
		this.setContentView(mView);
		this.show();

	}

	private void initView() {
		mCloseSb = (SeekBar) mView.findViewById(R.id.dimmer_pop_sb_close);
		mOpenSb = (SeekBar) mView.findViewById(R.id.dimmer_pop_sb_open);
		mDimmerSb = (SeekBar) mView.findViewById(R.id.dimmer_pop_sb_dimmer);

		mCloseTv = (TextView) mView.findViewById(R.id.dimmer_pop_tv_close);
		mOpenTv = (TextView) mView.findViewById(R.id.dimmer_pop_tv_open);
		mDimmerTv = (TextView) mView.findViewById(R.id.dimmer_pop_tv_dimmer);
		mDevName = (TextView) mView.findViewById(R.id.tv_title);

		mCancelBtn = (Button) mView.findViewById(R.id.btn_cancel);
		mOkBtn = (Button) mView.findViewById(R.id.btn_ok);
		mOkBtn.setText(R.string.OK);
		mCancelBtn.setText(R.string.Cancel);
		ImageButtonView backBtn = (ImageButtonView) mView.findViewById(R.id.btn_back);
		backBtn.setVisibility(View.GONE);

		mDevName.setText(mDimmer.getDeviceName());
	}

	private void initData() {
		if (mDimmer != null) {
			short turnSpeed = mDimmer.getTurnSpeed();

			mCloseSb.setProgress((turnSpeed >> 8) - 1);
			mOpenSb.setProgress((turnSpeed & 0xff) - 1);
			// mDimmer.turn(true);
			mDimmerSb.setProgress(mDimmer.getTurnDimmer());
			initSb();
		}
	}

	private void initSb() {
		mCloseTv.setText((mCloseSb.getProgress()+1) + Res.GetString(mContext, R.string.Minute));

		mOpenTv.setText((mOpenSb.getProgress() +1) + Res.GetString(mContext, R.string.Minute));

		mDimmerTv.setText(mDimmerSb.getProgress() * 100 / 1000 + "%");
	}

	private void initListener() {
		mCloseSb.setOnSeekBarChangeListener(mListener);
		mOpenSb.setOnSeekBarChangeListener(mListener);
		mDimmerSb.setOnSeekBarChangeListener(mListener);

		mOkBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				mDimmer.setTurnSpeed(new byte[] { (byte) (mOpenSb.getProgress()+1), (byte) (mCloseSb.getProgress()+1) });

				dismiss();
			}

		});

		mCancelBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	private OnSeekBarChangeListener mListener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			if (seekBar.getId() == R.id.dimmer_pop_sb_dimmer) {
				if(mDimmer != null){
					mDimmer.setDimmer(seekBar.getProgress(),true);
				}
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			mDimmer.onStartDimmer();
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			switch (seekBar.getId()) {
			case R.id.dimmer_pop_sb_close:
				mCloseTv.setText((progress+1) + Res.GetString(mContext, R.string.Minute));

				break;
			case R.id.dimmer_pop_sb_open:
				mOpenTv.setText((progress+1) + Res.GetString(mContext, R.string.Minute));

				break;
			case R.id.dimmer_pop_sb_dimmer:
				mDimmerTv.setText(progress * 100 / 1000 + "%");
				mDimmer.setDimmer(progress, false);
				break;
			}
		}
	};
}
